package com.zhang.crm.controller;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageInfo;
import com.zhang.crm.listener.UploadDataListener;
import com.zhang.crm.pojo.TClue;
import com.zhang.crm.result.R;
import com.zhang.crm.service.ClueService;
import jakarta.annotation.Resource;
import org.springframework.boot.context.event.SpringApplicationEvent;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
public class ClueController {

    @Resource
    private ClueService clueService;


    @GetMapping("api/getClues")
    public R getClueList(@RequestParam("current")Integer current) {
        PageInfo<TClue> info = clueService.getClueListByPage(current);
        return R.OK(info);
    }

    @PostMapping("api/addClue")
    public R addClue(TClue tClue) {
        Boolean b = clueService.addService(tClue);
        return b ? R.OK() : R.FAIL("添加失败");
    }

    @GetMapping("api/clueDetail/{id}")
    public R getClueById(@PathVariable("id")Integer id){
        TClue tClue = clueService.getClue(id);
        return R.OK(tClue);
    }

    @PutMapping("api/editClue")
    public R update(@RequestBody  TClue tClue){
        Boolean b = clueService.updateClueRemark(tClue);
        return b ? R.OK() : R.FAIL("更新失败");
    }

    @DeleteMapping("api/delClue/{clueId}")
    public R delActivity(@PathVariable("clueId")Integer clueId){
        Boolean b = clueService.delActivityById(clueId);
        return b ? R.OK() : R.FAIL("删除失败");
    }

    @DeleteMapping("api/clueDelBatch")
    public R delUserBatch(@RequestParam(value = "ids") List<String> ids){
        Boolean b = clueService.delClueBatch(ids);
        return b ? R.OK() : R.FAIL("删除失败");
    }

    /**
     * 导入Excel
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("api/importExcel")
    public R importExcel(MultipartFile file) throws IOException {
        System.out.println(file);
        clueService.importExcel(file.getInputStream());

        return R.OK();
    }

    @GetMapping("api/customers")
    public R getCustomerList(@RequestParam("current")Integer current) {
        PageInfo<TClue> info = clueService.getCustomerListByPage(current);
        return R.OK(info);
    }

}
